﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Collections.Specialized.BitVector32;

namespace DFGK.Service
{
    public class RoleService : BaseService, IRoleService
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly IRoleRepository _roleRepository;
        private readonly IUserRoleRepository _userRoleRepository;
        private readonly IMenuRepository _menuRepository;
        private readonly IMenuActionRepository _menuActionRepository;
        private readonly IActionRepository _actionRepository;
        private readonly IRoleMenuActionRepository _roleMenuActionRepository;
        public RoleService(IMapper mapper, IRoleRepository _roleRepository, IUnitOfWork unitOfWork, IUserRoleRepository userRoleRepository, IRoleMenuActionRepository roleMenuActionRepository, IMenuRepository menuRepository, IMenuActionRepository menuActionRepository, IActionRepository actionRepository) : base(mapper)
        {
            this._roleRepository = _roleRepository;
            _unitOfWork = unitOfWork;
            _userRoleRepository = userRoleRepository;
            _roleMenuActionRepository = roleMenuActionRepository;
            _menuRepository = menuRepository;
            _menuActionRepository = menuActionRepository;
            _actionRepository = actionRepository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="m"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool AddRole(Role m, out string message)
        {
            var role = _roleRepository.QueryIsEntityWhere(x => x.RoleCode == m.RoleCode);
            message=string.Empty;
            if (role!=null)
            {
                message = "编码已存在！";
                return false;
            }
            _roleRepository.AddEntity(m);
            return _unitOfWork.SaveChangesEntity();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool DelRole(long rid)
        {
            var roleobj=_roleRepository.QueryEntityById(rid);
            if(roleobj!=null)
            {
                //删除角色
                _roleRepository.DelEntity(roleobj);
                var urlist =  _userRoleRepository.GetEntityWhereAll(x => x.RoleId == rid);
                //删除用户角色关系表
                if (urlist.Any())
                    _userRoleRepository.DelEntityList(urlist.ToList());
                //查询角色菜单操作中该角色拥有的所有权限并删除
                var rmalist = _roleMenuActionRepository.GetEntityWhereAll(x => x.RoleId == rid);
                if (rmalist.Any())
                    _roleMenuActionRepository.DelEntityList(rmalist.ToList());
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <param name="rname"></param>
        /// <param name="state"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<Role> GetRoleList(string rname, int state, int pageIndex, int pageSize, out int totalCount)
        {
            var rlist = _roleRepository.GetEntityAll();
            //判断用户名称是否为空
            if (!string.IsNullOrEmpty(rname))
            {
                rlist = rlist.Where(x => x.RoleName.Contains(rname));
            }
            //判断用户状态
            if (state != 3)
            {
                rlist = rlist.Where(x => x.Status == state);
            }
            rlist = _roleRepository.GetEntityAlls(rlist, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return rlist.ToList();
        }

        /// <summary>
        /// 分配权限中的列表显示
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<MenuDto> GetRoleMenuActionList()
        {
            var mlist = _menuRepository.GetAllEntityList();
            return GetRoleMenuActionLists(mlist, 0);
        }
        public List<MenuDto> GetRoleMenuActionLists(List<Menu> list, long pid)
        {
            var alist = _menuActionRepository.GetAllEntityList();
            var malist = _actionRepository.GetAllEntityList();
            return list.Where(x => x.ParentId == pid).Select(x => new MenuDto
            {
                id = x.Id,
                ParentId = x.ParentId,
                MenuUrl = x.MenuUrl,
                Meta = new MetaDto
                {
                    MenuName = x.MenuName,
                    MenuIcon = x.MenuIcon,
                },
                Status = x.Status,
                DisplayOrder = x.DisplayOrder,
                children = GetRoleMenuActionLists(list, x.Id),
                Action = MenuActionById(alist, malist, x.Id)
            }).OrderBy(x => x.DisplayOrder).ToList();
        }

        public static List<ActionDto> MenuActionById(List<MenuAction> list, List<DFGK_WMS.Model.Models.Action> alist, long mid)
        {
            var lists = from a in list
                        join b in alist
                        on a.ActionId equals b.Id
                        where a.MenuId == mid
                        select b;
            return lists.Select(x => new ActionDto
            {
                Id = x.Id,
                ActionName = x.ActionName
            }).ToList();
        }
        /// <summary>
        /// 根据主键获取对象
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Role? GetRoleById(long rid)
        {
            return _roleRepository.QueryEntityById(rid);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="m"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public bool UpdRole(Role m, out string message)
        {
            var role = _roleRepository.QueryIsEntityWhere(x => x.RoleCode == m.RoleCode && x.Id!=m.Id);
            message = string.Empty;
            if (role != null)
            {
                message = "编码已存在！";
                return false;
            }
            _roleRepository.UpdEntity(m);
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
